package com.zhoug.zmui.chart.marker;

import android.annotation.SuppressLint;
import android.content.Context;
import android.widget.TextView;

import com.zhoug.common.utils.MathUtils;
import com.zhoug.zmui.R;
import com.zhoug.zmui.ZMUIShape;
import com.zhoug.zmui.chart.ChartEntry;
import com.zhoug.zmui.chart.ZMUIChart;
import com.zhoug.zmui.chart.pie.PieEntry;
import com.zhoug.zmui.chart.pie.ZMUIPieChartView;

/**
 * @Author 35574
 * @Date 2021/2/22
 * @Description
 */
@SuppressLint("ViewConstructor")
public class PieMarker extends MarkerView {

    private ZMUIShape mIvIcon;
    private TextView mTvLabel;
    private TextView mTvValue;
    private TextView mTvPercentage;


    public PieMarker(Context context, ZMUIPieChartView chart) {
        super(context, chart, R.layout.zmui_marker_pie);
        findViews();
    }

    @Override
    public void refreshContent(ChartEntry entry, int index) {
        float value = entry.getY();
        if(isIntegerValue()){
            mTvValue.setText(String.valueOf(((int)value)));
        }else{
            mTvValue.setText(String.valueOf(value));
        }
        ZMUIChart chart = getChart();
        if(chart instanceof ZMUIPieChartView){
            ZMUIPieChartView pieChartView= (ZMUIPieChartView) chart;
            int[] colors = pieChartView.getColors();
            mIvIcon.setShape(shape);
            if(colors!=null && colors.length>0){
                mIvIcon.setShapeColor(colors[index%colors.length]);
            }
            if(entry instanceof PieEntry){
                mTvLabel.setText(((PieEntry) entry).getLabel());
            }
            float totalValue = pieChartView.getTotalValue();
            float percent=value/totalValue;
            double decimal = MathUtils.getDecimal(percent * 100, getXiaoshu());
            mTvPercentage.setText(String.format("%s%%", decimal));
        }


        super.refreshContent(entry,index);
    }

    private void findViews() {
        mIvIcon = findViewById(R.id.iv_icon);
        mTvLabel = findViewById(R.id.tv_label);
        mTvValue = findViewById(R.id.tv_value);
        mTvPercentage = findViewById(R.id.tv_percentage);
    }
}
